/* $Id: HelloWorldExample.java,v 1.2 2001/11/29 18:27:25 remm Exp $
 *
 */
package autodiarywebserver;

import java.io.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.DataSource;

/**
 * The simplest possible servlet.
 *
 * @author James Duncan Davidson
 *  * 
 * cut down even smaller by CS Dept to be only the basics
 */

public class TestServlet extends HttpServlet {


    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        processRequest(request, response);
        /*
        try {
            processRequestPool(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
         * 
         */
    }
    

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        processRequest(request, response);
        
        /*
        try {
            processRequestPool(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
         * 
         */
    }

    public void processRequestPool(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException, NamingException, Exception
    {
        InitialContext ctx = new InitialContext();
        //DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
        Connection conn = null;
        Statement stmt = null;
        
        String sqlComm = "INSERT INTO acceData (acceX, acceY, acceZ, sensorTimeStamp, cpuTimeStamp) VALUES(1.1, 2.2, 9.9, 123000, 123000);";
        
        try{
            conn = ds.getConnection();

            stmt = conn.createStatement();
            stmt.executeUpdate(sqlComm);
            
            stmt.close();
            stmt = null;
            
            conn.close();
            conn = null;
        } finally{
            if (stmt != null){
                try{
                    stmt.close();
                } catch (SQLException sqlex){
                    // ignore
                }
                stmt = null;
            }
            
            if (conn != null){
                try {
                    conn.close();
                } catch (SQLException sqlex){
                    //ignore
                }
                conn = null;
            }
        }
    }
    
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        Connection connection = null;
        String sqlCommand = "";

        // get request infor
        //String strRow = request.getParameter("row1");
        //String[] arrRow = strRow.split("/s");
        String[] arrRow = new String[] {"1.1", "2.2", "9.9", "123000", "123000"};

        sqlCommand = "insert into acceData (acceX, acceY, acceZ, sensorTimeStamp, cpuTimeStamp) "
                    + "values(" + arrRow[0] + ", " + arrRow[1] + ", " + arrRow[2] + ", " 
                    + arrRow[3] + ", " + arrRow[4] + ");";

        try{
            String driverName = "com.mysql.jdbc.Driver";
            String dbUser = "thang";
            String dbPassword = "123";
            String dbName = "mysql";
            String host = "localhost";
            String url = "jdbc:mysql://" + host + "/" + dbName + "?user=" 
                    + dbUser + "&password=" + dbPassword;
            Class.forName(driverName).newInstance();
            connection = DriverManager.getConnection(url);
            Statement statement = connection.createStatement();
            
            statement.executeUpdate(sqlCommand);
        } catch(Throwable t){
            t.printStackTrace();
        }
        finally{
            try{
                connection.close();
            } catch(Throwable ignored){}
        }
        
        //Gets hold of the outputstream which can be used to
        //send data back to the browser (or whoever called 
        // the servlet).
        //PrintWriter printWriter = new PrintWriter( new OutputStreamWriter(response.getOutputStream()), true);
        
        //Tells the browser that we are sending data in HTML format
        //response.setContentType("text/html");
       
        // Send a small HTML document back
        //printWriter.println("Response from File Upload Servlet sql command: " + sqlCommand + "!");
        //----------------------------------------------------------
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");

        String title = new String("Welcome to Autodiary project");

        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1>" + title + "</h1>");
        out.println("This Servlet install a row to acceData table");
        out.println("Sql command: " + sqlCommand);
        out.println("</body>");
        out.println("</html>");
    }
}



